from PyQt4.QtGui import QWidget,QListWidgetItem,QFileDialog
from PyQt4 import QtCore
from KUI.KDatabaseUI.ui_KDatabaseWidget import Ui_KDatabaseWidget
from KDataBase.KDBManager import KDBManager
from KUtility.KConfig import KConfig

class KDatabaseWidget(QWidget):
    def __init__(self, parent=None):
        super(KDatabaseWidget, self).__init__(parent)
        self.ui =  Ui_KDatabaseWidget()
        self.ui.setupUi(self)  
        self.ui.BtnImport.clicked.connect(self.On_ClickImport)
        self.ui.BtnCleanupDataBase.clicked.connect(self.On_CleanUp)
        self.ui.BtnStopImport.clicked.connect(self.On_StopImport)
        self.ui.BtnConsolidate.clicked.connect(self.On_Consolidate)
        self.ui.BtnAutomaticImport.clicked.connect(self.On_OneClickImport)
        self.ui.BtnUpdate.clicked.connect(self.On_Update)
        self.dbManager=KDBManager(self)
        self.dbManager.ProgressUpdated.connect(self.On_UpdateProgress)
    def __delete__(self):
        if self.dbManager:
            self.dbManager=None
        
    def On_ClickImport(self):
        curprovider=self.ui.listWidgetProvider.currentItem()
        if not curprovider:
            return
        
        #filepath=self.ObtainFilePath(QFileDialog.Directory)
        #if filepath=="":
            #return
            
        if curprovider.text()=="EODData_Daily":
            filepath=KConfig.GetDatabaseSetting(r'Import Historical Daily Data/EOD Data/Import Path')
            self.dbManager.ExecuteImportCommand({"Op":"Import","Path":filepath,"Provider":'EOD'})
        elif curprovider.text()=="Stooq_Daily":
            filepath=KConfig.GetDatabaseSetting(r'Import Historical Daily Data/Stooq Data/Import Path')
            self.dbManager.ExecuteImportCommand({"Op":"Import","Path":filepath,"Provider":'Stooq'})
            
    def On_Update(self):
        curprovider=self.ui.listWidgetProvider.currentItem()
        if not curprovider:
            return
        
        filepath=self.ObtainFilePath(QFileDialog.AnyFile)
        if filepath=="":
            return
            
        if curprovider.text()=="EODData_Daily":
            self.dbManager.ExecuteImportCommand({"Op":"Update","Path":filepath,"Provider":'EOD'})
        elif curprovider.text()=="Stooq_Daily":
            self.dbManager.ExecuteImportCommand({"Op":"Update","Path":filepath,"Provider":'Stooq'}) 
            
    def On_StopImport(self):
        if self.dbManager:
            self.dbManager.StopImport()  
    def On_CleanUp(self):
        self.dbManager.ExecuteImportCommand({"Op":"Rebuild"})     
    def On_Consolidate(self):
        self.dbManager.ExecuteImportCommand({"Op":"Consolidate"})

    def On_OneClickImport(self):
        filepath=self.ObtainFilePath(QFileDialog.Directory)
        if filepath=="":
            return        
        self.dbManager.ExecuteImportCommand({"Op":"OneClickImport","Path":filepath})  
        
    def ObtainFilePath(self,mode):
        filedlg=QFileDialog(self)
        filedlg.setFileMode(mode)
        self.filedlg=filedlg
        if filedlg.exec_():  
            return str(filedlg.selectedFiles()[0])
        else:
            return ""
        
    def On_UpdateProgress(self,text):
        self.ui.listWidgetStatus.addItem(text)
        self.ui.listWidgetStatus.scrollToBottom()

                
        
        
        
     